package uk.ac.salfordcc.sccmobile;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import sccmobile.helper.*;
import sccmobile.helper.AlertDialogManager;
import sccmobile.helper.ConnectionDetector;

public class FeedbackActivity extends Activity {
    String savedText;
    EditText et;
    Button subBtn;

    ConnectionDetector cd;
    AlertDialogManager alert;

    private static final String URL = "http://si.salfordcc.ac.uk/ws/srv_staffAnnoucements.php";

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);

        et = (EditText)findViewById(R.id.feedbackText);
        subBtn = (Button)findViewById(R.id.submitFeedback);

        cd = new ConnectionDetector(getApplicationContext());
        alert = new AlertDialogManager();

        try{ savedText = savedInstanceState.getString("TMP_TXT"); }
        catch (Exception exc){ Log.e("scc No Saved Text", ""); }

        if(savedText != null && savedText != ""){
            et.setText(savedText);
        }

        //==== Set focus change listener method
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
            }
        });

        subBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitFeedbackData();
            }
        });
        setupActionBar();
	}

    public void submitFeedbackData(){
        SharedUtilities su = new SharedUtilities(getApplicationContext());
        EditText fbET = (EditText)findViewById(R.id.feedbackText);

        String username = su.getStoredPreference("USERNAME");   // Get username
        String feedback = fbET.getText().toString();            // Get feedback text

        if(!cd.isConnectingToInternet()){
            // Ensure that there is an internet connection available to send the feedback
            alert.showAlertDialog(this, "Internet Connection Error", "Please connect a working internet connection.", false);
            return;
        }
        else{


            Toast.makeText(this, username, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState){
        EditText et = (EditText)findViewById(R.id.feedbackText);
        savedInstanceState.putString("TMP_TXT", et.getText().toString());
        super.onSaveInstanceState(savedInstanceState);
    }


	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.feedback, menu);
		return true;
	}

    public boolean onOptionsItemSelected(MenuItem item){
        Intent intent;
        switch(item.getItemId()){
            case R.id.action_exitapp:
                SharedUtilities.endApplication(this, FeedbackActivity.this);
                return true;
            case R.id.action_settings:
                intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
                return true;
            case R.id.action_about:
                intent = new Intent(this, AboutThis.class);
                startActivity(intent);
                return true;
            case R.id.action_submit:
                submitFeedbackData();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}
